/**
 * Copyright (C) 2013-2014 the original author or authors.
 */
package com.foreveross.bsl.push.web;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.foreveross.bsl.common.utils.web.QueryFilter;
import com.foreveross.bsl.common.utils.web.QuerySpecs;
import com.foreveross.bsl.common.utils.web.model.PageMustache;
import com.foreveross.bsl.common.utils.web.model.QueryModelHelper;
import com.foreveross.bsl.common.utils.web.model.SelectBoxModel;
import com.foreveross.bsl.common.utils.web.model.SortInfoModel;
import com.foreveross.bsl.push.application.PushMgmtService;
import com.foreveross.bsl.push.application.vo.PushRequestVo;
import com.foreveross.bsl.push.application.vo.ReceiverTypeEnumVo;
import com.foreveross.bsl.system.application.ApplicationRegisterApplication;
import com.foreveross.bsl.web.ApplicationException;

/**
 * xxx
 *
 * @author Wangyi
 * @version v1.0
 *
 * @date 2013-9-23
 *
 */
@Controller
@RequestMapping(value = "/push")
public class PushRequestQueryController extends PushBaseController {
	
	@Inject
	private PushMgmtService pushMgmtService;
	
	@Inject
	private ApplicationRegisterApplication appRegClient;
	
	@RequestMapping(value = "")
	public String listRequest(@RequestParam(value="appId", required=false) String appId,
			@RequestParam(value = "page", defaultValue = "1") int pageNumber,
			@RequestParam(value = "page.size", defaultValue = PAGE_SIZE) int pageSize,
			Model model, HttpServletRequest request) {
		
		String queryUrl="/push";
		if(!StringUtils.isEmpty(appId)){
			queryUrl+="?appId="+appId;
		}
		QueryModelHelper queryHelper=new QueryModelHelper(queryUrl, null, makeSortInfo());
		QuerySpecs querySpecs=queryHelper.populateQueryModel(model, request);
		
		//处理查询提交日期：只查这一天的范围
		QueryFilter qfSubmitTime=querySpecs.getFilter("submitTime");
		if(qfSubmitTime!=null && QueryFilter.Operator.EQ.equals(qfSubmitTime.getOperator())){
			String date=qfSubmitTime.getValue().toString();
			date=date.substring(0, 10);
			querySpecs.removeFilter(qfSubmitTime);
			querySpecs.addFilter(QueryFilter.lte("submitTime", date+" 23:59:59"));
			querySpecs.addFilter(QueryFilter.gte("submitTime", date+" 00:00:00"));
		}
		Page<PushRequestVo> prs = null;
		if(StringUtils.isEmpty(appId)){
			prs=pushMgmtService.findPushRequests(pageNumber, pageSize, querySpecs);
		}
		else{
			String appKey=appRegClient.getAppKeyByAppId(appId);
			if(StringUtils.isEmpty(appKey)){
				throw new ApplicationException("根据appId获取appKey为空");
			}
			prs=pushMgmtService.findPushRequestsByApp(appKey, pageNumber, pageSize, querySpecs);
			model.addAttribute("appKey", appKey);
			model.addAttribute("appId", appId);
		}
		model.addAttribute("pushRequests", prs);
		model.addAttribute("page", new PageMustache(prs, PAGINATION_SIZE));
		
		String receiverType=null;
		QueryFilter qf=querySpecs.getFilter("receiverType");
		if(qf!=null){
			receiverType=qf.getValue().toString();
		}
		model.addAttribute("sendTypes", this.makeSendTypes(receiverType).getOptions());

		return "push/request-list";
	}
	
/*	private final SelectBoxModel makeQueryFields(){
		SelectBoxModel sbm=new SelectBoxModel();
		//option的key组成：操作表示符_条件字段，必须是操作表示符开始，
		//操作表示有EQ, LIKE, GT, LT, GTE, LTE 分别表示等于，like，大于，小于，大于等于，小于等于
		//条件字段为entity类的属性名或属性映射到mongoDB字段名，即@Field注解里的名字。
		sbm.addOption("GTE_submitTime", "提交时间", true);
		sbm.addOption("EQ_receiverType", "发送方式");
		return sbm;
	}*/
	
	private final SelectBoxModel makeSendTypes(String receiverType){
		SelectBoxModel sbm=new SelectBoxModel();
		sbm.addOption("", "所有", true);
		for(ReceiverTypeEnumVo rt : ReceiverTypeEnumVo.values()){
			sbm.addOption(rt.name(), "按"+rt.getDescription());
		}
		if(StringUtils.isNotBlank(receiverType)){
			sbm.setOptionSelected(receiverType);
		}
		return sbm;
	}
	
	private final SortInfoModel makeSortInfo(){
		SortInfoModel sim=new SortInfoModel();
		//排序字段为entity类映射到mongoDB字段名，即@Field注解里的名字。
		sim.setSortEntry("submit_time", "提交时间", false);
		return sim;
	}
	
}
